package com.sc.common.domain;


import com.sc.common.enums.HttpCodeEnum;

import java.io.Serializable;

/**
 * 通用的结果返回类
 *
 * @param <T>
 */
public class Result<T> implements Serializable {

    private String host;

    private Integer code;

    private String errorMessage;

    private T data;

    public Result() {
        this.code = 200;
    }

    public Result(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.errorMessage = msg;
        this.data = data;
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.errorMessage = msg;
    }

    public static Result error(int code, String msg) {
        Result result = new Result();
        return result.error(code, msg);
    }

    public static Result success(int code, String msg) {
        Result result = new Result();
        return result.ok(code, null, msg);
    }

    public static Result success(Object data) {
        Result result = setAppHttpCodeEnum(HttpCodeEnum.SUCCESS, HttpCodeEnum.SUCCESS.getErrorMessage());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static Result error(HttpCodeEnum enums) {
        return setAppHttpCodeEnum(enums, enums.getErrorMessage());
    }

    public static Result error(HttpCodeEnum enums, String errorMessage) {
        return setAppHttpCodeEnum(enums, errorMessage);
    }

    public static Result setAppHttpCodeEnum(HttpCodeEnum enums) {
        return success(enums.getCode(), enums.getErrorMessage());
    }

    private static Result setAppHttpCodeEnum(HttpCodeEnum enums, String errorMessage) {
        return success(enums.getCode(), errorMessage);
    }

    public Result<?> error(Integer code, String msg) {
        this.code = code;
        this.errorMessage = msg;
        return this;
    }

    public Result<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public Result<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.errorMessage = msg;
        return this;
    }

    public Result<?> ok(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }
    
}
